home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_12 / colvin / gc.h < prev    next >
C/C++ Source or Header  |  1995-10-10  |  1KB  |  46 lines

  1. ////////////////////////////////////////////////////////////
  2. // gc.h
  3. //
  4. // Smart pointers to garbage collected objects.
  5. #ifndef GC_H
  6. #define GC_H
  7.  
  8. #include "gc_imp.h"
  9.  
  10. // Allocator for garbage collected objects.
  11. template<class T> struct gc {
  12.    static void* allocate(size_t n) throw(bad_alloc) {
  13.       void* p= ::operator new(gc_data::min()+n);
  14.       return &(new(p) gc_value<T>())->data;
  15.    }
  16.    static void deallocate(T*) throw() {}
  17. };
  18.  
  19. // Smart pointers to collectable objects.
  20. template<class X> struct gc_ptr : gc_link {
  21.    gc_ptr() : gc_link() {}
  22.    gc_ptr(X* p) : gc_link(p) {}
  23.    gc_ptr(const gc_ptr& r) : gc_link(r) {}
  24.    gc_ptr& operator=(const gc_ptr& r) {
  25.       mark_or_copy(r);
  26.       return *this;
  27.    }
  28.    gc_ptr& operator=(X* p) {
  29.       set_ptr(p);
  30.       return *this;
  31.    }
  32.    operator X*()          const { return (X*)ptr; }
  33.    X* operator->()        const { return (X*)ptr; }
  34.    X& operator[](size_t i)      { return *((X*)ptr+i); }
  35.    X operator[](size_t i) const { return *((X*)ptr+i); }
  36. };
  37.  
  38. // Do garbage full or partial collection.
  39. void gc_all();
  40. void gc_min();
  41. void gc_handler() throw(bad_alloc);
  42.  
  43. #endif
  44.  
  45.  
  46.